layui.config({
    base : path+"/js"
}).use(['form','layer','jquery'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
        $ = layui.jquery;

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    });

    //登录按钮
   /* form.on("submit(login)",function(data){
        $(this).text("登录中...").attr("disabled","disabled").addClass("layui-disabled");
        setTimeout(function(){
            // window.location.href = "/layuicms2.0";
            login();
        },1000);
        return false;
    });*/

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    });

    //刷新验证码
    $('#chche').click(function (e) {
        $('#chche').attr("src", path+"/getCaptcha");
    });

    function login() {

        var username = $("#username").val();
        var password = $("#password").val();
        var validateCode = $("#validateCode").val();

        if (username == "") {
            //layui-layer layui-layer-dialog layui-layer-border layui-layer-msg
            $("#username").val("");
            $("#username").focus();
            layer.msg("用户名不能为空");

            return;
        }

        if (password == "") {
            $("#password").val("");
            $("#password").focus();
            layer.msg("密码不能为空");
            return;
        }

        if (validateCode == "") {

            $("#validateCode").val("");
            $("#validateCode").focus();
            layer.msg("验证码不能为空");
            return;
        }

        $.ajax({
            type: "post",
            async: false,
            data: "username=" + username + "&password=" + password + "&validateCode=" + validateCode, //$('#form1').serialize(),//
            url: path+"/login",
            dataType: "json",
            success: function (data) {
                if (data != '' && data != null) {
                    if (data.success) {
                        window.location.href = path+"/index";
                    } else {
                        if (data.message == '验证码错误') {
                            $('#chche').click();
                            $("#validateCode").val("");
                            $("#login").text("登录").removeAttr("disabled","disabled").removeClass("layui-disabled");
                        }
                        layer.msg(data.message);
                    }
                }
                console.log(data)
            },
            error: function (data) {
                console.log(data);
                $('#chche').click();
                $("#validateCode").val("");
                $("#login").text("登录").removeAttr("disabled","disabled").removeClass("layui-disabled");
                layer.msg("登录失败");
            }
        })
    }
})
